<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/26
 * Time: 14:14
 */

/**
 * Class Involved_UserModel
 */

class Involved_UserModel extends BaseModel{

    protected $table = 'user';

    public function GetUserInformation($user_id)
    {
        return DB::table($this->table)
            ->where(['user_id' => $user_id])
            ->first();
    }
    /**
     * 根据用户id更新用户信息
     * @param int $user_id
     * @param array $data
     * @param mixed
     */
    public function updateByUserId($user_id, $data) 
    {
        return DB::table($this->table)->where(['user_id' => $user_id])->update($data);
   	}

    /**
     * 投资记录
     * @return mixed
     * @author lzhp
     * @date 2016-05-03
     */
    public function UserInvestTicket($user_id)
    {
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where('user.user_id',$user_id)
            ->get();
    }
    /**
     * 投资记录收益完成的
     * @return mixed
     * @author lzhp
     * @date 2016-05-03
     */
    public function InvestTicketgains($user_id)
    {
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where(['user.user_id'=>$user_id,'invest_record.status'=>1])
            ->get();
    }
    /**
     * 投资记录金额待收益
     * @return mixed
     * @author lzhp
     * @date 2016-05-03
     */
    public function InvestTicketpende($user_id)
    {
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where(['user.user_id'=>$user_id,'invest_record.status'=>2])
            ->get();
    }
    

    /**
     * 投资记录分页
     * @param $user_id
     * @param $page
     * @return mixed
     */
    public function UserInvestTicketPage($user_id,$page)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where('user.user_id',$user_id)
            ->skip($limit)->take(10)
            ->get();
    }

    public function SelDate($user_id,$first,$end,$type,$page)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where(['user.user_id'=>$user_id,'invest_record.type'=>$type])
            ->whereBetween('invest_record.invest_time',array($first,$end))
            ->skip($limit)->take(10)
            ->get();
    }

    public function SelDatePage($user_id,$first,$end,$type){
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where(['user.user_id'=>$user_id,'invest_record.type'=>$type])
            ->whereBetween('invest_record.invest_time',array($first,$end))
            ->get();
    }
    public function SelDate2($user_id,$type,$page)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where(['user.user_id'=>$user_id,'invest_record.type'=>$type])
            ->skip($limit)->take(10)
            ->get();
    }

    public function SelDatePage2($user_id,$type){
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where(['user.user_id'=>$user_id,'invest_record.type'=>$type])
            ->get();
    }

//    public function SelDate3($user_id,$first,$end,$page)
//    {
//        $limit = ($page-1)*10;
//        return DB::table($this->table)
//            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
//            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
//            ->where(['user.user_id'=>$user_id])
//            ->whereBetween('invest_record.invest_time',array($first,$end))
//            ->skip($limit)->take(10)
//            ->get();
//    }
//
//    public function SelDatePage3($user_id,$first,$end){
//        return DB::table($this->table)
//            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
//            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
//            ->where(['user.user_id'=>$user_id])
//            ->whereBetween('invest_record.invest_time',array($first,$end))
//            ->get();
//    }

    /**
     * 报票记录
     * @return mixed
     * @author lzhp
     * @date 2016-05-03
     */
    public function UserBTicket($user_id)
    {
        return DB::table($this->table)
            ->join('ticket_record','ticket_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);
    }

    /**
     * 登陆验证
     * @param $name
     * @param $pwd
     * @return mixed
     */
    public function getUserInfo($name,$pwd)
    {
        return DB::table($this->table)
            ->join('user_extend','user_extend.user_id','=','user.user_id')
            ->where(['mobile'=>$name,'password'=>$pwd])
//            ->where(['user_name'=>$name,'password'=>$pwd])
            ->first();
    }

    /**
     *
     * @param $user_id
     * @return mixed
     */
    public function getUserSession($user_id)
    {
        return DB::table($this->table)
            ->join('user_extend','user_extend.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id])
            ->first();
    }

    /**
     * js登陆验证用户名
     * @param $name
     * @return mixed
     */
    public function JsCheckUser($name)
    {
        return DB::table($this->table)
            ->select('user_name','user_id','mobile')
          //  ->where('user_name',$name)
            ->where('mobile',$name)
            ->first();
    }

    /**
     * js登陆验证密码
     * @param $name
     * @return mixed
     */
    public function JsCheckPwd($name,$pwd)
    {
        return DB::table($this->table)
            ->select('password')
            ->where(['user_name'=>$name,'password'=>$pwd])
            ->orwhere(['mobile'=>$name,'password'=>$pwd])
            ->first();
    }
    /**
     * js注册验证用户名
     * @param $name
     * @return mixed
     */
    public function JsCheckUsername($name)
    {
        return DB::table($this->table)
            ->select('user_name')
            ->where('user_name',$name)
            ->first();
    }

    /**
     * js注册验证手机号
     * @param $mobile
     * @return mixed
     */
    public function JsCheckMobile($mobile)
    {
        return DB::table($this->table)
            ->select('mobile')
            ->where('mobile',$mobile)
            ->first();
    }

    /**
     * js注册验证邀请人手机号
     * @param $invite
     * @return mixed
     */
    public function JsCheckInvite($invite)
    {
        return DB::table($this->table)
            ->select('user_id')
            ->where('mobile',$invite)
            ->first();
    }

    /**
     * 账户明细
     * @param $user_id
     * @return mixed
     */
    public function Account($user_id)
    {
        return DB::table($this->table)
            ->join('account_record','account_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);

    }

    /**
     * 充值
     * @param $user_id
     * @return mixed
     */
    public function Recharge($user_id)
    {
        return DB::table($this->table)
            ->join('recharge_record','recharge_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);

    }

    /**
     * 提现
     * @param $user_id
     * @return mixed
     */
    public function WithDraw($user_id)
    {
        return DB::table($this->table)
            ->join('withdraw_record','withdraw_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);
    }

    /**
     * 登录密码重复判断
     * @param $pwd
     * @param $user_id
     * @return mixed
     */
    public function NowPwd($pwd,$user_id)
    {
        return DB::table($this->table)
            ->select('password')
            ->where(['password'=>$pwd,'user_id'=>$user_id])
            ->first();
    }

    public function NowPayPwd($pwd,$user_id)
    {
        return DB::table($this->table)
            ->where(['pay_pwd'=>$pwd,'user_id'=>$user_id])
            ->first();
    }
    /**
     * 消息列表
     * @param $user_id
     * @return mixed
     */
    public function ReadAllPage($user_id,$page)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id])
            ->skip($limit)->take(10)
            ->get();
    }
    public function ReadPage($user_id,$page)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'message.status'=>1])
            ->skip($limit)->take(10)
            ->get();
    }
    public function UnReadPage($user_id,$page)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'message.status'=>2])
            ->skip($limit)->take(10)
            ->get();
    }

    public function UnRead($user_id)
    {
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'message.status'=>2])
            ->get();
    }

    public function ReadAll($user_id)
    {
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->get();
    }

    public function Read($user_id)
    {
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'message.status'=>1])
            ->get();
    }
    /**
     * 删除多条消息
     * @param $num
     * @return mixed
     */
    public function UserNewsDel($num)
    {
        return DB::delete("delete from ypt_message where message_id in ($num)");
    }

    /**
     * 身份证是否被绑定
     * @param $id_card
     * @return mixed
     */
    public function CheckCard($id_card)
    {
        return DB::table($this->table)
            ->where('id_card',$id_card)
            ->first();
    }

    /**
     * 是否实名认证
     * @param $user_id
     * @return mixed
     */
    public function CheckIsCard($user_id)
    {
        return DB::table($this->table)
            ->select('id_card')
            ->where('user_id',$user_id)
            ->first();
    }

    /**
     * 忘记密码手机验证
     * @param $mobile
     * @return mixed
     */
    public function JsVerifyUserMobile($mobile)
    {
        return DB::table($this->table)
            ->select('mobile')
            ->where('mobile',$mobile)
            ->first();
    }

    /**
     * 重置密码
     * @param $pwd
     * @param $mobile
     * @return mixed
     */
    public function VerifySetPwd($pwd,$mobile)
    {
        return DB::table($this->table)
            ->select('mobile')
            ->where(['mobile'=>$mobile,'password' => $pwd])
            ->first();
    }

    /**
     * @param $user_id
     * @return mixed
     */
    public function UserName($user_id)
    {
        return DB::table($this->table)
            ->select('user_name','user_id')
            ->whereIn('user.user_id',$user_id)
            ->get();
    }

    /**
     * 实名认证是不是已经认证
     *
     */
    public function Certificate($user_id)
    {
        return DB::table($this->table)
            ->select('id_card')
            ->where(['user.user_id'=>$user_id])
            ->first();
    }
    /**
     * 实名认证完成 调取姓名和账号
     */
    public function Certificatename($user_id)
    {
        return DB::table($this->table)
            ->select('real_name','id_card','mobile')
            ->where(['user.user_id'=>$user_id])
            ->first();
    }


    /**
     * 查询邀请
     *
     */
    public  function Reward($user_id){
        return DB::table($this->table)

            ->join('invitie','invitie.bu_id','=','user.user_id')
            ->where(['invitie.u_id'=>$user_id])
            ->get();
    }
    /**
     * 邀请人总金额
     *
     */
    public  function Rewardsum($user_id){
        return DB::table($this->table)
            ->select('user_name','invitie.statue','invitie.reward','invitie.time')
            ->join('invitie','invitie.bu_id','=','user.user_id')
            ->where(['invitie.u_id'=>$user_id])
            ->sum('invitie.reward');
    }
    /**
     * 邀请人数
     *
     */
    public  function Rewardcount($user_id){
        return DB::table($this->table)
            ->select('user_name','invitie.statue','invitie.reward','invitie.time','invitie.t_id')
            ->join('invitie','invitie.bu_id','=','user.user_id')
            ->where(['invitie.u_id'=>$user_id])
            ->count('invitie.t_id');
    }

    /**
     * 数据加密
     * time:2016.05.17
     */
    public function UserInvite($input){
        $size = mcrypt_get_block_size ( MCRYPT_3DES, 'ecb' );
        $input =$this->pkcs5_pad($input, $size);
        $key = str_pad ( 'AQWSEDRFTGYHUJIKOLPXCVBN', 24, '0' );
        $td = mcrypt_module_open ( MCRYPT_3DES, '', 'ecb', '' );
        $iv = @mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
        @mcrypt_generic_init ( $td, $key, $iv );
        $data = mcrypt_generic ( $td, $input );
        mcrypt_generic_deinit ( $td );
        mcrypt_module_close ( $td );
        $data = base64_encode ( $data );
        $data = str_replace('+','&',$data);
        return $data;

    }

    public   function pkcs5_pad($text, $blocksize) {
        $pad = $blocksize - (strlen ( $text ) % $blocksize);
        return $text . str_repeat ( chr ( $pad ), $pad );
    }
    /**
     * 数据解密
     * time:2016.05.17
     */
    public    function pkcs5_unpad($text) {
        $pad = ord ( $text {strlen ( $text ) - 1} );
        //return $pad;
        if ($pad > strlen ( $text )) {
            return false;
        }
        if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) {
            return false;
        }
        return substr ( $text, 0, - 1 * $pad );
    }
    public  function UserInvitess($encrypted){
        $encrypted = str_replace('&','+',$encrypted);
        $encrypted = base64_decode ( $encrypted );
        //return $encrypted;
        $key = str_pad ( 'AQWSEDRFTGYHUJIKOLPXCVBN', 24, '0' );
        $td = mcrypt_module_open ( MCRYPT_3DES, '', 'ecb', '' );
        $iv = @mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
        $ks = mcrypt_enc_get_key_size ( $td );
        @mcrypt_generic_init ( $td, $key, $iv );
        $decrypted = mdecrypt_generic ( $td, $encrypted );
        mcrypt_generic_deinit ( $td );
        mcrypt_module_close ( $td );
        $y=$this->pkcs5_unpad( $decrypted );
        return $y;
    }

    /**
     * 现金卷
     * @param $user_id
     * @return mixed
     */
    public  function  voucher($user_id){
        return DB::table($this->table)
            ->select('reward','time','access','apply','statue','datetime','t_id')
            ->join('invitie','invitie.u_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id])
            ->get();
    }
    /**
     * 现金卷总数
     * @param $user_id
     * @return mixed
     */
    public  function  vouchersum($user_id){

        return DB::table($this->table)
            ->select('reward','time','access','apply','statue','datetime','t_id')
            ->join('invitie','invitie.u_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'invitie.statue'=>0])
            ->sum('reward');
    }
    /**
     * 体验金
     * @param $user_id
     * @return mixed
     */
    public  function  experience($user_id){
        return DB::table($this->table)
            ->select('time','access','statue','scale','datetime')
            ->join('exper_scale','exper_scale.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id])
            ->get();
    }
    /**
     * 体验金m没有过期
     * @param $user_id
     * @return mixed
     */
    public  function  experiencenot($user_id){
        return DB::table($this->table)
            ->select('time','t_id','access','statue','scale','datetime')
            ->join('exper_scale','exper_scale.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'exper_scale.statue'=>0])
            ->get();
    }
    /**
     * 有效体验金总数
     * @param $user_id
     * @return mixed
     */
    public  function  experiencesum($user_id){

        return DB::table($this->table)
            ->select('time','access','statue','scale','datetime')
            ->join('exper_scale','exper_scale.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'exper_scale.statue'=>0])
            ->sum('scale');
    }
    /**
     * 体验金收益金额
     * @param $user_id
     * @return mixed
     */
    public  function  experiencemoney($user_id){

        return DB::table($this->table)
            ->select('exper_money','exper_nums','exper_time','exper_annua')
            ->join('exper_record','exper_record.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id])
            ->get();
    }
    /**
     *投资记录金额
     */
    public  function InvestTicketnum($user_id){
        return DB::table($this->table)
            ->select('invest_money')
            ->join('invest_record','invest_record.user_id','=','user.user_id')
            ->where(['invest_record.user_id'=>$user_id])
            ->sum('invest_money');

    }

     /**
     *收益算法函数
     */
     public function Algorithm($moneys,$nums,$yiled){
            $shouyi = ($moneys * $nums * $yiled)/100;
            // return $shouyi;
            $year=date("Y");//判断年份
            if(($year%4==0)&&($year%100!=0)||($year%400==0)){
            $gains=$shouyi/366;
            return $gains;
            }else{
            $gains=$shouyi/365;
            return  $gains;
            }

     }

    /**
     *总收益算法
     */
     public function CountAlgorithm($moneys,$nums,$yiled){
            $shouyi = ($moneys * $nums * $yiled)/100;
            // return $shouyi;
            $year=date("Y");//判断年份
            if(($year%4==0)&&($year%100!=0)||($year%400==0)){
            $gains=($shouyi/366)+$moneys;
            return $gains;
            }else{
            $gains=($shouyi/365)+$moneys;
            return  $gains;
            }

     }
    /**
     *个人中心收益算法
     */
     public function UserAlgorithm($str,$expersum,$sum){
            //$sum冷冻金额  $str收益  $expersum体验金
            $year=date("Y");//判断年份
            if(($year%4==0)&&($year%100!=0)||($year%400==0)){
            $gains=($str/366)+$sum;
            return  round($gains,2);
            }else{
            $gains=($str/365)+$sum;
            return  round($gains,2);
            }

     }

    /**
     * 查询随机数
     * @param $user_id
     * @return mixed
     */
     public function CheckPayPwd($user_id)
     {
         return DB::table($this->table)
             ->select('pay_num')
             ->where(['user_id'=>$user_id])
             ->first();
     }

    /**
     * 验证支付密码
     * @param $pay_pwd
     * @return mixed
     */
    public function CheckBankPwd($pay_pwd)
    {
        return DB::table($this->table)
            ->select('pay_pwd')
            ->where(['pay_pwd'=>$pay_pwd])
            ->first();
    }

}